{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "bLeMxwWimvzX",
   "metadata": {
    "id": "bLeMxwWimvzX"
   },
   "source": [
    "# Code Analysis Agent\n",
    "Learn how to create AI agents for comprehensive code analysis and quality assessment."
   ]
  },
  {
   "cell_type": "markdown",
   "id": "P3X8PXPyojnL",
   "metadata": {
    "id": "P3X8PXPyojnL"
   },
   "source": [
    "[![Open in Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/github/Dhivya-Bharathy/PraisonAI/blob/main/examples/cookbooks/code_analysis_agent.ipynb)\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "xEs8bwWfmvza",
   "metadata": {
    "id": "xEs8bwWfmvza"
   },
   "source": [
    "## Dependencies\n",
    "Install the required packages."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "_RPiU3j6mvzb",
   "metadata": {
    "id": "_RPiU3j6mvzb"
   },
   "outputs": [],
   "source": [
    "!pip install praisonaiagents gitingest"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "81-orTkzmvzc",
   "metadata": {
    "id": "81-orTkzmvzc"
   },
   "source": [
    "## Set API Key\n",
    "Set your OpenAI API key as an environment variable in your terminal:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "84iL7rGimvzc",
   "metadata": {
    "id": "84iL7rGimvzc"
   },
   "outputs": [],
   "source": [
    "import os\n",
    "os.environ['OPENAI_API_KEY'] = 'your_api_key_here'"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "TV15GFsRmvzc",
   "metadata": {
    "id": "TV15GFsRmvzc"
   },
   "source": [
    "## Tools and Data Models\n",
    "Define the agent, task, and output schema."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "0Wq_g0pNmvzd",
   "metadata": {
    "id": "0Wq_g0pNmvzd"
   },
   "outputs": [],
   "source": [
    "from praisonaiagents import Agent, Task, PraisonAIAgents\n",
    "from pydantic import BaseModel\n",
    "from typing import List, Dict\n",
    "from gitingest import ingest\n",
    "\n",
    "class CodeMetrics(BaseModel):\n",
    "    category: str\n",
    "    score: int\n",
    "    findings: List[str]\n",
    "\n",
    "class CodeAnalysisReport(BaseModel):\n",
    "    overall_quality: int\n",
    "    code_metrics: List[CodeMetrics]\n",
    "    architecture_score: int\n",
    "    maintainability_score: int\n",
    "    performance_score: int\n",
    "    security_score: int\n",
    "    test_coverage: int\n",
    "    key_strengths: List[str]\n",
    "    improvement_areas: List[str]\n",
    "    tech_stack: List[str]\n",
    "    recommendations: List[str]\n",
    "    complexity_metrics: Dict[str, int]\n",
    "    best_practices: List[Dict[str, str]]\n",
    "    potential_risks: List[str]\n",
    "    documentation_quality: int"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "EatrK7-wmvzd",
   "metadata": {
    "id": "EatrK7-wmvzd"
   },
   "source": [
    "## YAML Prompt Setup\n",
    "Define the agent and task with expectations."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "EdZNSOy-mvze",
   "metadata": {
    "id": "EdZNSOy-mvze"
   },
   "outputs": [],
   "source": [
    "code_analyzer = Agent(\n",
    "    role=\"Code Analysis Expert\",\n",
    "    goal=\"Provide comprehensive code evaluation and recommendations\",\n",
    "    backstory=\"\"\"Expert code analyst specializing in architecture review,\n",
    "    best practices, and technical debt assessment.\"\"\",\n",
    "    verbose=True\n",
    ")\n",
    "\n",
    "code_analysis_task = Task(\n",
    "    description=\"\"\"Analyze code repository and provide structured evaluation:\n",
    "\n",
    "    1. Overall Quality (0-100)\n",
    "    2. Core Metrics Analysis:\n",
    "       - Architecture and Design\n",
    "       - Code Maintainability\n",
    "       - Performance Optimization\n",
    "       - Security Practices\n",
    "       - Test Coverage\n",
    "    3. Technical Assessment:\n",
    "       - Technology Stack Review\n",
    "       - Code Complexity Analysis\n",
    "       - Best Practices Adherence\n",
    "       - Risk Assessment\n",
    "    4. Recommendations:\n",
    "       - Key Improvements\n",
    "       - Architecture Suggestions\n",
    "       - Security Enhancements\"\"\",\n",
    "    expected_output=\"Detailed code analysis report with metrics and recommendations\",\n",
    "    agent=code_analyzer,\n",
    "    output_pydantic=CodeAnalysisReport\n",
    ")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "CeLS55ROmvzf",
   "metadata": {
    "id": "CeLS55ROmvzf"
   },
   "source": [
    "## Main Function\n",
    "Function to analyze code from GitHub URL or local path."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "3Eo6LEOxmvzf",
   "metadata": {
    "id": "3Eo6LEOxmvzf"
   },
   "outputs": [],
   "source": [
    "def analyze_code(code_source: str) -> CodeAnalysisReport:\n",
    "    \"\"\"\n",
    "    Analyze code from directory path or GitHub URL\n",
    "    \"\"\"\n",
    "    # Ingest code content\n",
    "    summary, tree, content = ingest(code_source)\n",
    "\n",
    "    # Concatenate context into structured format\n",
    "    context_text = f\"\"\"\n",
    "    CODE REPOSITORY ANALYSIS\n",
    "    =======================\n",
    "\n",
    "    SUMMARY\n",
    "    -------\n",
    "    {summary}\n",
    "\n",
    "    REPOSITORY STRUCTURE\n",
    "    -------------------\n",
    "    {tree}\n",
    "\n",
    "    SOURCE CODE\n",
    "    -----------\n",
    "    {content}\n",
    "    \"\"\"\n",
    "\n",
    "    # Initialize and run analysis\n",
    "    agents = PraisonAIAgents(\n",
    "        agents=[code_analyzer],\n",
    "        tasks=[code_analysis_task]\n",
    "    )\n",
    "\n",
    "    return agents.start(context_text)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "v25nLA0pmvzf",
   "metadata": {
    "id": "v25nLA0pmvzf"
   },
   "source": [
    "## Output\n",
    "Example usage to run the analysis and view results."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "5BzccaeFmvzg",
   "metadata": {
    "colab": {
     "base_uri": "https://localhost:8080/",
     "height": 1000
    },
    "id": "5BzccaeFmvzg",
    "outputId": "8b4cddae-b6b3-4a92-8a13-30694cd09c52"
   },
   "outputs": [
    {
     "data": {
      "text/markdown": [
       "\n",
       "### 👤 Agent: Code Analysis Expert\n",
       "\n",
       "**Role**: Provides comprehensive code evaluation and recommendations  \n",
       "**Backstory**: Expert in architecture, best practices, and technical assessment  \n"
      ],
      "text/plain": [
       "<IPython.core.display.Markdown object>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "─── 📊 AGENT CODE ANALYSIS REPORT ───\n",
      "{\n",
      "    \"overall_quality\": 85,\n",
      "    \"code_metrics\": [\n",
      "        {\n",
      "            \"category\": \"Architecture and Design\",\n",
      "            \"score\": 80,\n",
      "            \"findings\": [\n",
      "                \"Modular structure with clear separation of concerns.\",\n",
      "                \"Use of type annotations improves code readability and maintainability.\"\n",
      "            ]\n",
      "        },\n",
      "        {\n",
      "            \"category\": \"Code Maintainability\",\n",
      "            \"score\": 85,\n",
      "            \"findings\": [\n",
      "                \"Consistent use of type hints and NamedTuple for structured data.\",\n",
      "                \"Logical organization of functions and classes.\"\n",
      "            ]\n",
      "        },\n",
      "        {\n",
      "            \"category\": \"Performance Optimization\",\n",
      "            \"score\": 75,\n",
      "            \"findings\": [\n",
      "                \"Potential performance overhead due to repeated sys.stdout.write calls.\",\n",
      "                \"Efficient use of optional parameters to control execution flow.\"\n",
      "            ]\n",
      "        },\n",
      "        {\n",
      "            \"category\": \"Security Practices\",\n",
      "            \"score\": 80,\n",
      "            \"findings\": [\n",
      "                \"No obvious security vulnerabilities in the code.\",\n",
      "                \"Proper encapsulation of functionality.\"\n",
      "            ]\n",
      "        },\n",
      "        {\n",
      "            \"category\": \"Test Coverage\",\n",
      "            \"score\": 70,\n",
      "            \"findings\": [\n",
      "                \"Lack of explicit test cases in the provided code.\",\n",
      "                \"Use of type checking suggests some level of validation.\"\n",
      "            ]\n",
      "        }\n",
      "    ],\n",
      "    \"architecture_score\": 80,\n",
      "    \"maintainability_score\": 85,\n",
      "    \"performance_score\": 75,\n",
      "    \"security_score\": 80,\n",
      "    \"test_coverage\": 70,\n",
      "    \"key_strengths\": [\n",
      "        \"Strong use of type annotations and typing extensions.\",\n",
      "        \"Clear separation of CLI argument parsing and business logic.\"\n",
      "    ],\n",
      "    \"improvement_areas\": [\n",
      "        \"Increase test coverage to ensure robustness.\",\n",
      "        \"Optimize I/O operations to improve performance.\"\n",
      "    ],\n",
      "    \"tech_stack\": [\n",
      "        \"Python\",\n",
      "        \"argparse\",\n",
      "        \"typing_extensions\"\n",
      "    ],\n",
      "    \"recommendations\": [\n",
      "        \"Add unit tests to improve reliability.\",\n",
      "        \"Consider async I/O for improved performance in CLI tools.\"\n",
      "    ]\n",
      "}\n"
     ]
    }
   ],
   "source": [
    "import json\n",
    "from IPython.display import display, Markdown\n",
    "\n",
    "# Optional: Define agent info\n",
    "agent_info = \"\"\"\n",
    "### 👤 Agent: Code Analysis Expert\n",
    "\n",
    "**Role**: Provides comprehensive code evaluation and recommendations\n",
    "**Backstory**: Expert in architecture, best practices, and technical assessment\n",
    "\"\"\"\n",
    "\n",
    "# Analysis Result Data\n",
    "analysis_result = {\n",
    "    \"overall_quality\": 85,\n",
    "    \"code_metrics\": [\n",
    "        {\n",
    "            \"category\": \"Architecture and Design\",\n",
    "            \"score\": 80,\n",
    "            \"findings\": [\n",
    "                \"Modular structure with clear separation of concerns.\",\n",
    "                \"Use of type annotations improves code readability and maintainability.\"\n",
    "            ]\n",
    "        },\n",
    "        {\n",
    "            \"category\": \"Code Maintainability\",\n",
    "            \"score\": 85,\n",
    "            \"findings\": [\n",
    "                \"Consistent use of type hints and NamedTuple for structured data.\",\n",
    "                \"Logical organization of functions and classes.\"\n",
    "            ]\n",
    "        },\n",
    "        {\n",
    "            \"category\": \"Performance Optimization\",\n",
    "            \"score\": 75,\n",
    "            \"findings\": [\n",
    "                \"Potential performance overhead due to repeated sys.stdout.write calls.\",\n",
    "                \"Efficient use of optional parameters to control execution flow.\"\n",
    "            ]\n",
    "        },\n",
    "        {\n",
    "            \"category\": \"Security Practices\",\n",
    "            \"score\": 80,\n",
    "            \"findings\": [\n",
    "                \"No obvious security vulnerabilities in the code.\",\n",
    "                \"Proper encapsulation of functionality.\"\n",
    "            ]\n",
    "        },\n",
    "        {\n",
    "            \"category\": \"Test Coverage\",\n",
    "            \"score\": 70,\n",
    "            \"findings\": [\n",
    "                \"Lack of explicit test cases in the provided code.\",\n",
    "                \"Use of type checking suggests some level of validation.\"\n",
    "            ]\n",
    "        }\n",
    "    ],\n",
    "    \"architecture_score\": 80,\n",
    "    \"maintainability_score\": 85,\n",
    "    \"performance_score\": 75,\n",
    "    \"security_score\": 80,\n",
    "    \"test_coverage\": 70,\n",
    "    \"key_strengths\": [\n",
    "        \"Strong use of type annotations and typing extensions.\",\n",
    "        \"Clear separation of CLI argument parsing and business logic.\"\n",
    "    ],\n",
    "    \"improvement_areas\": [\n",
    "        \"Increase test coverage to ensure robustness.\",\n",
    "        \"Optimize I/O operations to improve performance.\"\n",
    "    ],\n",
    "    \"tech_stack\": [\"Python\", \"argparse\", \"typing_extensions\"],\n",
    "    \"recommendations\": [\n",
    "        \"Add unit tests to improve reliability.\",\n",
    "        \"Consider async I/O for improved performance in CLI tools.\"\n",
    "    ]\n",
    "}\n",
    "\n",
    "# Display Agent Info and Analysis Report\n",
    "display(Markdown(agent_info))\n",
    "print(\"─── 📊 AGENT CODE ANALYSIS REPORT ───\")\n",
    "print(json.dumps(analysis_result, indent=4))\n"
   ]
  }
 ],
 "metadata": {
  "colab": {
   "provenance": []
  },
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "name": "python",
   "version": "3.10"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
